package com.telnetnz.ticketdesk.action.acp.tag;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.telnetnz.ticketdesk.action.base.BasicSessionPageAcpAction;
import com.telnetnz.ticketdesk.bean.Tag;
import com.telnetnz.ticketdesk.service.OperationLogService;
import com.telnetnz.ticketdesk.service.TagService;
import com.telnetnz.ticketdesk.service.TicketToTagService;

public class AcpTagAction extends BasicSessionPageAcpAction
{
	private static Logger logger = Logger.getLogger(AcpTagAction.class);

	public String doList()
	{
		List<Tag> tags = TagService.getAllTags();
		this.put_page_key("tags", tags);
		return SUCCESS;
	}

	public String doDelete()
	{
		RequestBean bean = new DefaultRequestBean();
		int id = bean.getInt("id");
		Tag tag = TagService.getTag(id);
		if (tag == null)
		{
			return message_box(
					langRes.get("global.error"), 
					langRes.get("tag.tag-not-exist-redirect", new String[]{bean.getRedirect()}),
					bean.getRedirect(),
					3
				);
		}

		if (bean.isSubmitted())
		{
			if (bean.get("confirm").length() > 0)
			{
				TagService.delete(tag);

				OperationLogService.addLog(
						OperationLogService.OBJECT_TYPE_TAG,
						tag.getId(), 
						getSession().getSession_uuid(), 
						getContext().getActionName(),
						null);

				return message_box(
						langRes.get("global.operation-success"), 
						langRes.get("tag.tag-delete-success-redirect", new String[]{bean.getRedirect()}),
						bean.getRedirect(),
						2
					);
			}
			else
			{
				return this.redirect(bean.getRedirect());
			}
		}

		Map<String, String> form = new HashMap<String, String>();
		form.put("id", ""+ id);
		form.put("redirect", bean.getRedirect());
		return confirm_box(
				langRes.get("global.confirm"), 
				langRes.get("global.confirm-message"),
				baseLink(), 
				null, 
				form);
	}

	public String doEdit()
	{
		TagEditBean bean = new TagEditBean();

		if (bean.getTag() == null)
			return this.message_box(
					langRes.get("global.operation-fail"),
					langRes.get("tag.tag-not-exist-redirect", new String[]{bean.getRedirect()}),
					bean.getRedirect(),
					2);

		if (bean.isSubmitted() && bean.validate())
		{
			if(bean.getTag().getId() == 0)
			{
				TagService.add(bean.getTag());

				OperationLogService.addLog(
						OperationLogService.OBJECT_TYPE_TAG,
						bean.getTag().getId(),
						getSession().getSession_uuid(),
						getContext().getActionName(),
						OperationLogService.createSimpleDiff("name", new String[]{"null", bean.getTag().getName()}));

				return this.message_box(
						langRes.get("global.operation-success"), 
						langRes.get("tag.tag-add-success-redirect", new String[]{bean.getRedirect()}),
						bean.getRedirect(),
						2);
			}
			else
			{
				Map<String, List<String>> diff = bean.getTag().compare();
				TagService.update(bean.getTag());

				OperationLogService.addLog(
						OperationLogService.OBJECT_TYPE_TAG,
						bean.getTag().getId(),
						getSession().getSession_uuid(),
						getContext().getActionName(),
						diff);

				return this.message_box(
						langRes.get("global.operation-success"), 
						langRes.get("tag.tag-update-success-redirect", new String[]{bean.getRedirect()}),
						bean.getRedirect(),
						2);
			}
		}

		this.put_page_key("bean", bean);
		this.put_page_key("post_action", baseLink());

		return INPUT;
	}

	public class TagEditBean extends RequestBean
	{
		private Tag tag;

		public Tag getTag() {
			return tag;
		}

		@Override
		protected void loadRequest() 
		{
			int id = getInt("id");
			if (id > 0)
			{
				tag = TagService.getTag(id);
			}
			else
			{
				tag = new Tag();
				tag.initialize();
			}

			if (tag != null)
			{
				if (isSubmitted())
				{
					tag.setName(get("tag_name"));
				}
			}
		}

		@Override
		protected boolean validate() 
		{
			boolean flag = true;
			if (tag.getName().length() == 0)
			{
				flag = false;
				put_error_msg("tag_name", langRes.get("global.content-cannot-be-empty"));
			}
			else
			{
				Tag dummy = TagService.getTagByName(tag.getName());
				if (dummy != null && (tag.getId() == 0 || tag.getId() != dummy.getId()))
				{
					flag = false;
					put_error_msg("tag_name", langRes.get("tag.tag-already-exist"));
				}
			}

			return flag;
		}
	}
}
